import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provide/provide.dart';
import 'package:study_flutter/yzhg/dartTools/YEventBusUtil.dart';
import 'package:study_flutter/yzhg/project/shop/content/Content.dart';
import 'package:study_flutter/yzhg/project/shop/event/YEventBusBean.dart';
import 'package:study_flutter/yzhg/project/shop/model/YCatagoryBean.dart';
import 'package:study_flutter/yzhg/project/shop/provide/ChildCategory.dart';

import 'CategoryEventBean.dart';

class YRightCatGeGory extends StatefulWidget {
  @override
  _YRightCatGeGoryState createState() => _YRightCatGeGoryState();
}

class _YRightCatGeGoryState extends State<YRightCatGeGory> {
  int currentIndex = 0;
  StreamSubscription<YEventBusBean> _event;

  @override
  void initState() {
    super.initState();
    _event = YEventBusUtil.getInstance().on<YEventBusBean>().listen((data) {
      if (data.eventPosition == Content.EVENT_CATEGORY_TITLE) {
        setState(() {
          currentIndex = data.eventIng;
        });
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Provide<ChildCategory>(builder: (context, child, childList) {
      return Container(
        height: ScreenUtil().setHeight(80),
        width: ScreenUtil().setWidth(570),
        decoration: BoxDecoration(
            color: Colors.white,
            border:
                Border(bottom: BorderSide(width: 1, color: Colors.black12))),
        child: ListView.builder(
            //设置横向的ListView
            scrollDirection: Axis.horizontal,
            itemCount: childList.childCategoryList.length,
            itemBuilder: (context, index) {
              return _rightInkWell(childList.childCategoryList[index], index);
            }),
      );
    });
  }

  Widget _rightInkWell(BxMallSubDto item, int index) {
    return InkWell(
      onTap: () {
        //点击事件
        YEventBusUtil.getInstance().fire(
            CategoryEventBean(item.mallCategoryId, item.mallSubId, true, 1));
        //标记当前选择的是哪一个
        setState(() {
          currentIndex = index;
        });
      },
      child: Container(
        height: ScreenUtil().setHeight(80),
        //width: ScreenUtil().setWidth(160),
        padding: EdgeInsets.only(left: 12, right: 12),
        alignment: Alignment.center,
        child: Text(
          item.mallSubName,
          textAlign: TextAlign.center,
          style: TextStyle(
              fontSize: ScreenUtil().setSp(28),
              color: (currentIndex == index) ? Colors.pink : Colors.black),
        ),
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
    _event.cancel();
  }
}
